package com.zrrd.yt.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yt.common.result.Result;
import com.zrrd.yt.model.product.SkuInfo;
import com.zrrd.yt.model.product.SpuImage;
import com.zrrd.yt.model.product.SpuSaleAttr;
import com.zrrd.yt.product.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    //分页查询sku
    @GetMapping("/list/{page}/{limit}")
    public Result<Page> page(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit) {
        Page<SkuInfo> pageInfo = new Page<>(page, limit);
        skuInfoService.page(pageInfo,null);
        return Result.ok(pageInfo);
    }

    //sku上架
    @GetMapping("/onSale/{skuId}")
    public Result<String> onSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(1);
        return skuInfoService.updateById(skuInfo)?Result.ok("成功"):Result.fail("失败");
    }

    //sku下架
    @GetMapping("/cancelSale/{skuId}")
    public Result<String> cancelSale(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        skuInfo.setIsSale(0);
        return skuInfoService.updateById(skuInfo)?Result.ok("成功"):Result.fail("失败");
    }

    //查询spu的图片
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId){
        return Result.ok(skuInfoService.getSpuImageList(spuId));
    }

    //查询spu的销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId){
        return Result.ok(skuInfoService.getSpuSaleAttrList(spuId));
    }

    //保存sku
    @PostMapping("/saveSkuInfo")
    public Result<String> saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return Result.ok("成功");
    }
}
